from typing import Any
from .utils import verify_token
from rest_framework import status
from django.http import JsonResponse

class LoginMiddleWare:
    def __init__(self,get_response) -> None:
        self.get_response = get_response

    def __call__(self,request) -> Any:
        #判断访问的是不是用户相关接口
        if request.path.startswith('/api/user'):
            d = request.META.get('HTTP_AUTHORIZATION')
            #如果不是用户相关接口
            if not verify_token(d):
                return JsonResponse({
                    "code":status.HTTP_401_UNAUTHORIZED,
                    'msg':'请登录'
                },json_dumps_params={'ensure_ascii':False})
        return self.get_response(request)